package com.anyisn.anyflickr.shell.model
{
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.vo.PhotoListVO;
	import com.anyisn.flickr.as3.patterns.proxy.FlickrPhotosProxy;
	
	public class ListPhotosProxy extends FlickrPhotosProxy
	{
		public static const NAME:String = 'ListPhotosProxy';
		
		public function ListPhotosProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		override protected function onInterestingnessGetList(event:FlickrResultEvent) : void {
			super.onInterestingnessGetList(event);
			if(event.success) {
				lastList = currentList;
				currentList = new PhotoListVO(ShellFacade.TILE_INTERESTINGNESS_GET_LIST, event.data.photos);
				sendNotification(ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS, event.data.photos);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.TILE_INTERESTINGNESS_GET_LIST_FAILED);
			}
		}
		
		override protected function onPhotosSearch(event:FlickrResultEvent) : void {
			super.onPhotosSearch(event);
			if(event.success) {
				lastList = currentList;
				currentList = new PhotoListVO(ShellFacade.TILE_PHOTOS_SEARCH, event.data.photos);
				currentList.searchVO = searchVO;
				sendNotification(ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS, event.data.photos);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.TILE_PHOTOS_SEARCH_FAILED);
			}
		}
		
		override protected function onPeopleGetPublicPhotos(event:FlickrResultEvent) : void {
			super.onPeopleGetPublicPhotos(event);
			if(event.success) {
				lastList = currentList;
				currentList = new PhotoListVO(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS, event.data.photos);
				currentList.nsid = nsid;
				sendNotification(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS, event.data.photos);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_FAILED);
			}
		}
		
		override protected function onGetFavoritesList(event:FlickrResultEvent) : void {
			super.onGetFavoritesList(event);
			if(event.success) {
				lastList = currentList;
				currentList = new PhotoListVO(ShellFacade.TILE_FAVORITES_GET_LIST, event.data.photos);
				sendNotification(ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS, event.data.photos);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.TILE_FAVORITES_GET_LIST_FAILED);
			}
		}
		
		public function getPreviousList():void {
			if(lastList) {
				switch(lastList.methodName) {
					case ShellFacade.TILE_INTERESTINGNESS_GET_LIST:
						sendNotification(ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS, lastList.photos);
						break;
					case ShellFacade.TILE_PHOTOS_SEARCH:
						sendNotification(ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS, lastList.photos);
						break;
					case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS:
						sendNotification(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS, lastList.photos);
						break;
					case ShellFacade.TILE_FAVORITES_GET_LIST:
						sendNotification(ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS, lastList.photos);
						break;
				}
				var temp:PhotoListVO = currentList;
				currentList = lastList;
				lastList = temp;
			}
		}
		
		public function changeListPage(page:uint):void {
			if(currentList) {
				switch(currentList.methodName) {
					case ShellFacade.TILE_INTERESTINGNESS_GET_LIST:
						getInterestingnessList(page);
						break;
					case ShellFacade.TILE_PHOTOS_SEARCH:
						search(searchVO, page);
						break;
					case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS:
						getPeoplePublicPhotos(nsid, page);
						break;
					case ShellFacade.TILE_FAVORITES_GET_LIST:
						getFavoritesList(page);
						break;
				}
			}
		}
		
		override protected function get perpage() : uint {
			var proxy:SettingsProxy = facade.retrieveProxy(SettingsProxy.NAME) as SettingsProxy;
			return proxy.tilePerpage;
		}
	}
}